#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

template<class K, class V>
class AVLtreeNode
{
public:
	pair<K, V> _kv;
	AVLtreeNode<K, V>* _right;
	AVLtreeNode<K, V>* _left;
	AVLtreeNode<K, V>* _parent;

	int _bf;

	AVLtreeNode(const pair<K, V> kv) : _kv(kv), _right(nullptr), _left(nullptr), _parent(nullptr), _bf(0)
	{}
};


template<class K, class V>
class AVLtree
{
private:
	typedef AVLtreeNode<K, V> Node;
	Node* _root = nullptr;

public:
	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		if (parent->_kv.first > kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		while (parent)
		{

			if (cur == parent->_left)
			{
				parent->_bf--;
			}
			else 
			{
				parent->_bf++;
			}

			if (parent->_bf == 0)
			{
				// 更新结束
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				// 继续往上更新
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				// 子树不平衡了，需要旋转
				if (parent->_bf == 2 && cur->_bf == 1)
				{
					RotateL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == -1)
				{
					RotateR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)
				{
					RotateRL(parent);
				}
				

				break;
			}
			else
			{
				assert(false);
			}
		}

		return true;
	}


	//左单旋：parent->right = cur->left
	//        cur->left = paerent
	//cur是指被插入的元素
	void RotateL(Node* parent)
	{
		Node* cur = parent->_right;
		Node* curleft = cur->_left;

		//改指向关系是双向的,改父节点指向的子节点同时要改子节点指向的父节点

		parent->_right = curleft;
		if (curleft)
		{
			curleft->_parent = parent;
		}

		cur->_left = parent;
		Node* ppnode = parent->_parent;
		parent->_parent = cur;

		if (parent == _root)
		{
			_root = cur;
			cur->_parent = nullptr;
		}
		else
		{
			if (ppnode->_left == parent)
			{
				ppnode->_left == cur;
				cur->_parent = ppnode;
			}
			else
			{
				ppnode->_right == cur;
				cur->_parent = ppnode;
			}
		}
		parent->_bf = cur->_bf = 0;
	}


	//右单旋：parent->left = cur->right
	//        cur->right = paerent
	//cur是指被插入的元素
	void RotateR(Node* parent)
	{
		Node* cur = parent->_left;
		Node* curright = cur->_right;

		parent->_left = curright;
		if (curright)
			curright->_parent = parent;

		Node* ppnode = parent->_parent;
		cur->_right = parent;
		parent->_parent = cur;

		if (ppnode == nullptr)
		{
			_root = cur;
			cur->_parent = nullptr;
		}
		else
		{
			if (ppnode->_left == parent)
			{
				ppnode->_left = cur;
			}
			else
			{
				ppnode->_right = cur;
			}

			cur->_parent = ppnode;
		}

		parent->_bf = cur->_bf = 0;
	}

	void RotateRL(Node* parent)
	{
		Node* cur = parent->_right;
		Node* curleft = cur->_left;
		int bf = curleft->_bf;

		RotateR(parent->_right);
		RotateL(parent);

		//考虑子树的左孩子的平衡因子的情况
		if (bf == 0)
		{
			cur->_bf = 0;
			curleft->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == 1)
		{
			cur->_bf = 0;
			curleft->_bf = 0;
			parent->_bf = -1;
		}
		else if (bf == -1)
		{
			cur->_bf = 1;
			curleft->_bf = 0;
			parent->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}

	void RotateLR(Node* parent)
	{
		Node* cur = parent->_left;
		Node* curright = cur->_right;
		int bf = curright->_bf;

		RotateL(parent->_left);
		RotateR(parent);

		if (bf == 0)
		{
			parent->_bf = 0;
			cur->_bf = 0;
			curright->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 1;
			cur->_bf = 0;
			curright->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			cur->_bf = -1;
			curright->_bf = 0;
		}

		int Height()
		{
			return Height(_root);
		}

		int Height(Node * root)
		{
			if (root == nullptr)
				return 0;

			int leftHeight = Height(root->_left);
			int rightHeight = Height(root->_right);

			return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
		}

		bool IsBalance()
		{
			return IsBalance(_root);
		}

		bool IsBalance(Node * root)
		{
			if (root == nullptr)
				return true;

			int leftHight = Height(root->_left);
			int rightHight = Height(root->_right);

			if (rightHight - leftHight != root->_bf)
			{
				cout << "平衡因子异常:" << root->_kv.first << "->" << root->_bf << endl;
				return false;
			}

			return abs(rightHight - leftHight) < 2
				&& IsBalance(root->_left)
				&& IsBalance(root->_right);
		}
	}
};


